package com.portal.cwegportal.controller;

import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.app.CurrentUser;
import com.portal.cwegportal.input.menu.*;
import com.portal.cwegportal.input.menu.*;
import com.portal.cwegportal.model.User;
import com.portal.cwegportal.service.MenusService;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/services/portal/index")
@Api("门户首页API")
public class IndexController {
    @Autowired
    MenusService MenusService;

    @ApiOperation(value = "创建菜单", notes = "创建菜单", produces = "application/json")
    @PostMapping("/createmenu")
    public ResponseEntity<JsonResult> createMenu(@RequestBody CreateMenuInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"创建菜单",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(MenusService.CreateMenu(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "删除菜单", notes = "删除菜单",produces = "application/json")
    @PostMapping("/deletemenu")
    public ResponseEntity<JsonResult> deleteMenu(@RequestBody DeleteMenuInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"删除菜单",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(MenusService.DeleteMenu(input));

        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "修改菜单", notes = "修改菜单",produces = "application/json")
    @PostMapping("/editmenu")
    public ResponseEntity<JsonResult> editMenu(@RequestBody EditMenuInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"修改菜单",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(MenusService.EditMenu(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "搜索菜单", notes = "搜索菜单",produces = "application/json")
    @PostMapping("/getmenus")
    public ResponseEntity<JsonResult> getMenus(@RequestBody GetMenusInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"搜索菜单",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(MenusService.GetMenus(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "加载菜单信息", notes = "加载菜单信息",produces = "application/json")
    @PostMapping("/pageLoad")
    public ResponseEntity<JsonResult> pageLoad(@RequestBody PageLoadInput input) throws Exception {
        BusinessLogger.Info( LoggerGenre.Add,"加载菜单信息",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(MenusService.PageLoad(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "登陆用户", notes = "登陆用户",produces = "application/json")
    @PostMapping("/loginUser")
    public ResponseEntity<JsonResult> LoginUser(@RequestBody User user) throws Exception {

        JsonResult r = new JsonResult();
        try {
            CurrentUser.setUserId(user.getUserId());
            CurrentUser.setUserCode(user.getUserCode());
            CurrentUser.setUserName(user.getUserName());
            r.setResult(user);
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }
    @ApiOperation(value = "得到登陆用户", notes = "得到登陆用户",produces = "application/json")
    @PostMapping("/getLoginUser")
    public ResponseEntity<JsonResult> GetLoginUser() throws Exception {

        JsonResult r = new JsonResult();
        try {
            List<User> lu=new ArrayList<>();
            lu.add(new User());
            lu.add(new User());
            BusinessLogger.Info(LoggerGenre.DataSynchronize,"测试日志",lu);
            User user=new User();
            user.setUserId(CurrentUser.getUserId());
            user.setUserCode(CurrentUser.getUserCode());
            user.setUserName(CurrentUser.getUserName());
            r.setResult(user);
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
//        try{
            Integer a=1,b=0;
            Integer c=a/b;
//        }catch (Exception ex){
//
//        }



        return ResponseEntity.ok(r);
    }

}


